package com.hbx.platform.auth.controller;

import com.hbx.platform.auth.context.LoginContext;
import com.hbx.platform.auth.service.IMenuService;
import com.hbx.platform.auth.domain.Menu;
import com.hbx.platform.auth.query.MenuQuery;
import com.hbx.platform.base.utils.AjaxResult;
import com.hbx.platform.base.utils.PageList;
import com.hbx.platform.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    public IMenuService menuService;

    @Autowired
    public RedisTemplate<String,Object> redisTemplate;


    /**
     * 保存和修改公用的
     *
     * @param menu 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Menu menu) {
        try {
            if (menu.getId() != null) {
                menuService.update(menu);
                return AjaxResult.me().setMessage("修改成功");
            } else {
                menuService.save(menu);
                return AjaxResult.me().setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("报错：" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            menuService.delete(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Menu menu = menuService.queryById(id);
            return AjaxResult.me().setData(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult getAll() {
        try {
            List<Menu> list = menuService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult page(@RequestBody MenuQuery query) {
        try {
            PageList<Menu> pageList = menuService.page(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult batchDelete(List<Long> ids){
        try {
            Integer count = menuService.batchDelete(ids);
            return AjaxResult.me().setMessage("删除了"+count+"条记录");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }

    @GetMapping("/dynamic")
    public AjaxResult getDynamicMenus(HttpServletRequest request){
        try {
            String token = request.getHeader("TOKEN");
//            Employee employee = (Employee) LoginContext.context.get(token);
            Employee employee = (Employee) redisTemplate.opsForValue().get(token);
            List<Menu> list = menuService.getDynamicMenus(employee.getId());
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取动态菜单失败");
        }
    }

    @PostMapping("/tree")
    public AjaxResult tree(){
        try {
            List<Menu> list = menuService.tree();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取菜单树失败");
        }
    }
}
